<?php
namespace Api\Controller;
use Think\Controller;
use Think\Vender;
use Think\Model;
class IndexController extends Controller {
    public function index()
    {
        echo "index";
    }
    /**
     * 最新首页图片请求
     */
    public function get_image_list(){
        $fenlei = M("fenlei")->order("sort asc,id asc")->select();
		foreach ($fenlei as $key => $value) {
            if($value["id"]==1){
                $product = M("product")->where(array("fenlei_id"=>$value["id"]))->order("sort asc,id asc")->select();
                foreach ($product as $ke => $val) {
                    $img=M('product')->alias('a')
                       ->join('__GOODS_CAR_TYPE__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.car_img');

                    $name=M('product')->alias('a')
                       ->join('__GOODS_CAR_TYPE__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.car_name');

                       $product[$ke]['name']=$name;
                       $product[$ke]['image']=$img;
                }

               $fenlei[$key]["product_list"] = $product;
            }elseif ($value["id"]==2) {
                  $product = M("product")->where(array("fenlei_id"=>$value["id"]))->order("sort asc,id asc")->select();
                  foreach ($product as $ke => $val) {
                    $img=M('product')->alias('a')
                       ->join('__GOODS_PARTS__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.parts_logo');

                    $name=M('product')->alias('a')
                       ->join('__GOODS_PARTS__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.parts_jianxie');

                       $product[$ke]['name']=$name;
                       $product[$ke]['image']=$img;
                }

               $fenlei[$key]["product_list"] = $product;
            }elseif ($value["id"]==3) {
                  $product = M("product")->where(array("fenlei_id"=>$value["id"]))->order("sort asc,id asc")->select();
                  foreach ($product as $ke => $val) {
                    $img=M('product')->alias('a')
                       ->join('__GOODS_INSURANCELIST__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.logo');

                    $name=M('product')->alias('a')
                       ->join('__GOODS_INSURANCELIST__ as b on a.goods_id=b.id')
                       ->where(['b.id'=>$product[$ke]['goods_id']])
                       ->getField('b.is_name');

                       $product[$ke]['name']=$name;
                       $product[$ke]['image']=$img;
                }

               $fenlei[$key]["product_list"] = $product;
            }
		}
        //var_dump($fenlei);exit();
		echo $this->ajaxReturn($fenlei);

    }
     /**
     * 热门品牌
     */
    public function get_hotcar_list(){
        $hotcar=M('hotcar')->alias('a')
        ->field('a.id,a.car_type_id,b.logo,b.name')
        ->join('__CAR_TYPE__ as b on a.car_type_id=b.id')
        ->where($where)
        ->select();
        echo  $this->ajaxReturn($hotcar);
    }
    /**
     * 车型列表
     */
    public function get_cartype_list(){
        $Model=new Model();
        // $cartype=$Model->query('select pinyin,group_concat(id),group_concat(name),group_concat(logo) from cgj_car_type group by pinyin');
        // foreach ($cartype as $key => $value) {
        //     $data[$key]['pinyin']=$cartype[$key]['pinyin'];
        //     $data[$key]['id']=explode(",",$cartype[$key]['group_concat(id)']);
        //     $data[$key]['name']=explode(",",$cartype[$key]['group_concat(name)']);
        //     $data[$key]['logo']=explode(",",$cartype[$key]['group_concat(logo)']);
        // }
        // print_r($data);
        $cartype=$Model->query('select pinyin from cgj_car_type group by pinyin');
        foreach ($cartype as $key => $value) {
            $data[$key]['pinyin']=$cartype[$key]['pinyin'];
            $data[$key]['sub']=M('car_type')->where(array('pinyin'=>$data[$key]['pinyin']))->select();

        }
        echo  $this->ajaxReturn($data);
    }
    /**
     * 品牌列表
     */
    public function get_goodcar_list(){
        $typeid=I('get.typeid');
        $id=I('get.id');
        if(!empty($typeid)){
             $data=M('goods_car_type')->where(array('car_type_id'=>$typeid))->select();
             $this->ajaxReturn($data);
        }
        if(!empty($id)){
             $data=M('goods_car_type')->where(array('id'=>$id))->select();
             foreach ($data as $key => $value) {
                $data[$key]['car_broadcast']=json_decode($data[$key]['car_broadcast']);
                $data[$key]['car_imgs']=json_decode($data[$key]['car_imgs']);

             }
             $this->ajaxReturn($data);
        }
    }
     /**
     * 顾客咨询
     */
     public function get_zixun_list(){
        // $id=I('get.id');
        // $data=M('goods_car_type')->field('id,car_type_id,logo_name,car_img,car_name,car_pirce,shop_id')->where(array('id'=>$id))->select();
        // foreach ($data as $key => $value) {
        //         $re=json_decode($data[$key]['shop_id'],true);
        //      foreach ($re as $ke => $val) {
        //          $data[$key]['shop'][$ke]=M('shop')->where(array('id'=>$val))->find();
        //      }
        // }
        //$this->ajaxReturn($data);
        $id=I('get.id');
        $data=M('goods_car_type')->field('id,car_type_id,logo_name,car_img,car_name,car_pirce,shop_id')->where(array('id'=>$id))->find();
        $re=json_decode($data['shop_id'],true);
        foreach ($re as $ke => $val) {
            $data['shop'][$ke]=M('shop')->where(array('id'=>$val))->find();
        }
        $this->ajaxReturn($data);
     }

    /**
     * 顾客咨询资料提交
     */
    public function get_zixun_submit(){
        $id=I('post.id');
        $data['name']=I('post.name');
        $data['moblie']=I('post.moblie');
        $data['address']=I('post.address');
        $data['shop_id']=json_encode(explode(",",$id));
        $data['car_id']=I('post.carid');
        $re=M('customer')->add($data);
        if($re){
            $qustion['code']=200;
            $qustion['msg']="信息提交成功";
        }
        $this->ajaxReturn($qustion);
    }
    /**
     * 配件列表
     */
    public function get_parts_list(){
        $data=M('goods_parts')->select();
        $this->ajaxReturn($data);
    }

     /**
     * 商铺详情
     */
     public function get_partsdetail(){
        $id=I('get.id');
        $re=M('goods_parts')->where(['id'=>$id])->select();
        foreach ($re as $key => $value) {
            $re[$key]['parts_img']=json_decode($re[$key]['parts_img']);
            $re[$key]['parts_zizhi']=json_decode($re[$key]['parts_zizhi']);
        }
        $this->ajaxReturn($re);
     }

     /**
     * 保险种类列表及商品详情
     */
     public function get_insurance_type(){
        $data=M('insurance_type')->select();
         $this->ajaxReturn($data);
     }

     public function goods_insurance_min(){
         $id=M('insurance_type')->min('id');
         $data=M('goods_insurancelist')->where(['insurance_type_id'=>$id])->select();
         foreach ($data as $key => $value) {
            $data[$key]['imgs']=json_decode($data[$key]['imgs']);
        }
         $this->ajaxReturn($data);
     }

    /**
     * 保险商品详情
     */
       public function get_goods_insurance(){
        $id=I('get.id');
        $data=M('insurance_type')->alias('a')
        ->join("__GOODS_INSURANCELIST__ as b on a.id=b.insurance_type_id")
        ->where(['insurance_type_id'=>$id])
        ->select();
        foreach ($data as $key => $value) {
            $data[$key]['imgs']=json_decode($data[$key]['imgs']);
        }
         $this->ajaxReturn($data);
     }
    /**
     * 点击保险图片跳转链接
     */
     public function get_insurance_detail(){
        $id=I('get.id');
        $data=M('goods_insurancelist')->where(['id'=>$id])->find();
        $data['imgs']=json_decode($data['imgs']);
        $this->ajaxReturn($data);
     }
    /**
     * 获取购物车商品
     */
    public function get_shopcar_list(){
        $data=M('shopcar')->select();
         $this->ajaxReturn($data); 
    }
     /**
     * 获取购物车商品
     */
     public function get_stock_count(){
        $shopcar_id=I('post.id');
        $count=I('post.count');
        $surplus=M('stock')->where(['shopcar_id'=>$shopcar_id])->find();
        $sum=intval($surplus['surplus'])-intval($count);
        if($sum < 0){
            $data['surplus']=$surplus;
            $data['msg']='该商品已无库存!';
            $this->ajaxReturn($data); 
        }
     }
}